package pl.nazaweb.jboot.afk;

import java.awt.AWTException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Marek Kawczyński
 */
public class FishingCallback implements ICallback {

    private Rectangle fishingSpace;
    private int direction = 1;
    private int fishingRoodHotKey;
    private Point fishingScreanKeyboardPoint;
    private Robot robot;

    public FishingCallback(Rectangle fishingSpace, int direction, int fishingRodHotKey) throws AWTException {
        this.fishingSpace = fishingSpace;
        this.direction = direction;
        this.fishingRoodHotKey = fishingRodHotKey;
        robot = new Robot();
    }

    public FishingCallback(Rectangle fishingSpace, int direction, Point fishingRodHotKey) throws AWTException {
        this.fishingSpace = fishingSpace;
        this.direction = direction;
        this.fishingScreanKeyboardPoint = fishingRodHotKey;
        robot = new Robot();
    }

    @Override
    public void execute() {
        for (int x = 0; x < 10; x++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(FishingCallback.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (fishingScreanKeyboardPoint != null) {
                robot.mouseMove(fishingScreanKeyboardPoint.x, fishingScreanKeyboardPoint.y);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            } else {
                robot.keyPress(fishingRoodHotKey);
            }
            Point fishingPoint = getRandomFishingPoint();
            robot.mouseMove(fishingPoint.x, fishingPoint.y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            if (fishingScreanKeyboardPoint == null) {
                robot.keyRelease(fishingRoodHotKey);
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(FishingCallback.class.getName()).log(Level.SEVERE, null, ex);
        }
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    private Point getRandomFishingPoint() {
        int x = (int) getRandomNumberInRange(fishingSpace.getX(), fishingSpace.getX() + fishingSpace.getWidth()) - 10;
        int y = (int) getRandomNumberInRange(fishingSpace.getY(), fishingSpace.getY() + (direction * fishingSpace.getHeight())) - 10;
        return new Point(x, y);
    }

    private double getRandomNumberInRange(double min, double max) {
        return min + (Math.random() * (max - min));
    }

    public Rectangle getFishingSpace() {
        return fishingSpace;
    }

    public void setFishingSpace(Rectangle fishingSpace) {
        this.fishingSpace = fishingSpace;
    }
}
